//
//  UILabel+AP.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/5/20.
//

import Foundation
import UIKit

extension UILabel: APCompatible {}
public extension APBase where Base: UILabel {
    
    /// 类方法初始化
    @discardableResult
    static func lb(_ font: UIFont, _ textColor: UIColor, numberOfLines: Int = 1) -> UILabel {
        let lb = UILabel()
        lb.font = font
        lb.textColor = textColor
        return lb
    }
    
    /// 文字大小自适应
    /// 参考链接：[UIBaselineAdjustment 的作用](https://blog.csdn.net/qq_37333981/article/details/109355675)
    var adjustsToFitWith: Bool {
        set {
            base.adjustsFontSizeToFitWidth = newValue
            if newValue {
                base.baselineAdjustment = .alignCenters
            } else {
                base.baselineAdjustment = .alignBaselines
            }
        }
        get {
            base.adjustsFontSizeToFitWidth
        }
    }
    
    
}
 
//!!!: 链式语法
public extension UILabel {
    @discardableResult
    func text(_ text: String?) -> UILabel {
        self.text = text
        return self
    }
    
    @discardableResult
    func font(_ font: UIFont) -> Self {
        self.font = font
        return self
    }
    
    @discardableResult
    func textColor(_ textColor: UIColor) -> UILabel {
        self.textColor = textColor
        return self
    }
    
    @discardableResult
    func numberOfLines(_ numberOfLines: Int) -> UILabel {
        self.numberOfLines = numberOfLines
        return self
    }
    
    @discardableResult
    func textAlignment(_ textAlignment: NSTextAlignment) -> UILabel {
        self.textAlignment = textAlignment
        return self
    }
}

